﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml.Serialization;

namespace DataProcess
{
    [Serializable]
    public class Paragraph
    {
        #region 私有字段定义
        private string _releaseContent = null;
        private string _content = null;
        private string _orgContent = null;
        private Article article;
        private ParagraphList children;
        #endregion

        #region 不进行XML序列化的属性

        /// <summary>
        /// 段落的唯一标记
        /// </summary>
        [XmlIgnore]
        public object Identity { get; set; }

        /// <summary>
        /// 父级段落
        /// </summary>
        [XmlIgnore]
        public Paragraph Parent { get; set; }

        /// <summary>
        /// 发布库版本内容
        /// </summary>
        [XmlIgnore]
        public string ReleaseContent
        {
            get
            {
                if (_releaseContent == null)
                {
                    _releaseContent = replaceNotPublish(Content, "<br />");
                }
                return _releaseContent;
            }
        }

        /// <summary>
        /// 原始段落内容
        /// </summary>
        [XmlIgnore]
        public string OrgContent
        {
            get
            {
                return _orgContent;
            }
            set
            {
                _orgContent = value;
                _content = _releaseContent = null;
            }
        }

        #endregion

        /// <summary>
        /// 段落标题
        /// </summary>
        public string Title { get; set; }

        [XmlIgnore]
        public Article Article
        {
            get
            {
                if (this.article == null)
                {
                    this.article = this.FindArticle();
                }
                return this.article;
            }
        }

        /// <summary>
        /// html格式内容
        /// </summary>
        public string Content
        {
            get
            {
                if (_content == null)
                {
                    _content = GetHtmlContent(OrgContent);
                }
                return _content;
            }
            set { _content = value; }
        }

        /// <summary>
        /// 子段落
        /// </summary>
        public ParagraphList Children
        {
            get
            {
                if (this.children == null)
                {
                    this.children = new ParagraphList(this);
                }
                return this.children;
            }
            set
            {
                Children.Clear();
                Children.AddRange(value);
            }
        }


        public Paragraph()
        {
            Identity = Guid.NewGuid().ToString();
        }
        public Paragraph(Article article)
            : this()
        {
            this.article = article;
        }


        internal void AddToArticle()
        {
            if (this.Article != null)
            {
                this.Article.allParagraph.Add(this.Identity, this);
                if (this.children != null && this.children.Count > 0)
                {
                    foreach (var child in children)
                    {
                        child.AddToArticle();
                    }
                }
            }
        }

        private Article FindArticle()
        {
            Paragraph paragraph = this;
            while (paragraph.Parent != null)
            {
                paragraph = paragraph.Parent;
            }
            return paragraph.article;
        }

        public string replaceNotPublish(string content, string lineFlag)
        {
            if (string.IsNullOrEmpty(content))
            {
                return string.Empty;
            }
            return Regex.Replace(content, string.Format("※.*?※|◆.*?(?={0})|◆[^◆]*$", lineFlag), string.Empty);//<br */?>
        }

        /// <summary>
        /// 把文本内容转换为HTML内容
        /// </summary>
        /// <param name="textContent">文本内容</param>
        /// <returns></returns>
        public string GetHtmlContent(string textContent)
        {
            if (string.IsNullOrEmpty(textContent))
            {
                return textContent;
            }
            string content = Regex.Replace(textContent, "<img[^<>]*\\s+src=(?<Quote>[\\\"']?)(?<value>.+?)\\k<Quote>\\s+[^<>]*?>", "[img:$2]", RegexOptions.IgnoreCase);
            List<string> tables = new List<string>();
            //处理表格
            content = Regex.Replace(content, "<table.*?>.*?</table>", match =>
            {
                string c = string.Format("[table:{0}]", tables.Count);
                tables.Add(match.Value);
                return c;
            }, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            content = Regex.Replace(content, "<sub>(.*?)</sub>", "★$1★", RegexOptions.IgnoreCase);
            content = Regex.Replace(content, "<sup>(.*?)</sup>", "☆$1☆", RegexOptions.IgnoreCase);

            content = HttpUtility.HtmlEncode(content);
            content = content.Replace(" ", "&nbsp;");

            content = content.Replace("\r\n", Title == "附药" ? "|" : "<br />");

            content = Regex.Replace(content, "★(.*?)★", "<sub>$1</sub>");
            content = Regex.Replace(content, "☆(.*?)☆", "<sup>$1</sup>");
            for (int i = 0; i < tables.Count; i++)
            {
                content = content.Replace(string.Format("[table:{0}]", i), tables[i]);
            }
            content = Regex.Replace(content, "\\[img:([^\\[\\]]*)?\\]", "<img src=\"$1\" />", RegexOptions.IgnoreCase);


            return content;
        }
    }
}
